package user;
import dataProces.DBS;
import dataProces.FileOpera;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Objects;

public class WordLink extends JDialog implements ActionListener {
    JPanel[] jPanels = new JPanel[4];
    JTextField[] jTextFields = new JTextField[3];
    JButton[] jButtons = new JButton[4];
    String[] prompt = {"查看典故", "提交成语", "退出接龙", "导出接龙", "机器", "提示", "玩家", "成语接龙(字)"};
    String last = null;
    static HashMap<String, String> idiom = new HashMap<>();
    static LinkedList<String> idioms = new LinkedList<>();
    int[] count = {0, 0};

    public WordLink() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(6, 1));
        for (int i = 0; i < jTextFields.length; i++) {
            if (i % 2 == 0)
                jTextFields[i] = new JTextField(20);
            else jTextFields[i] = new JTextField(30);
            if (i < jTextFields.length - 1)
                jTextFields[i].setEditable(false);
        }
        for (int i = 0; i < jButtons.length; i++) {
            jButtons[i] = new JButton(prompt[count[0]++]);
            jButtons[i].addActionListener(this);
        }
        for (int i = 0; i < jPanels.length; i++) {
            jPanels[i] = new JPanel();
            if (i < jPanels.length - 1) {
                jPanels[i].add(new JLabel(prompt[count[0]++]));
                jPanels[i].add(jTextFields[i]);
                if (i % 2 == 0)
                    jPanels[i].add(jButtons[count[1]++]);
            } else {
                jPanels[i].add(jButtons[count[1]++]);
                jPanels[i].add(jButtons[count[1]++]);
            }
            container.add(jPanels[i]);
        }
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(500, 300);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - 500) / 2, (screen.height - 300) / 2);
        setTitle(prompt[count[0]++]);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButtons[0]) {
            new IdiomShow(idiom);
        } else if (e.getSource() == jButtons[1]) {
            DBS dbs = new DBS();
            if (dbs.dataQueries("idiom", "idiom", jTextFields[jTextFields.length - 1].getText().trim()) == null)
                jTextFields[1].setText("查无此词，再想过一个成语吧！");
            else {
                idiom = dbs.dataQueries("idiom", "idiom", jTextFields[jTextFields.length - 1].getText().trim());
                if (last != null && !Objects.equals(idiom.get("first_word"), last)) {
                    jTextFields[1].setText("没接上，再想过一个成语吧！");
                } else {
                    idioms.addLast(idiom.get("idiom"));
                    if (dbs.dataQueries("idiom", "first_word", idiom.get("last_word")) == null) {
                        jTextFields[1].setText("接不下去了，你赢了！");
                        new FileOpera().exportTxt(idioms,this.getClass().getName());
                    } else {
                        idiom = dbs.dataQueries("idiom", "first_word", idiom.get("last_word"));
                        last = idiom.get("last_word");
                        idioms.addLast(idiom.get("idiom"));
                        jTextFields[0].setText(idioms.getLast());
                        jTextFields[1].setText("接龙成功！");
                        jTextFields[jTextFields.length - 1].setText(null);
                    }
                }
            }
        } else if (e.getSource() == jButtons[2]) {
            dispose();
        } else if (e.getSource() == jButtons[3]) {
            new FileOpera().exportTxt(idioms,this.getClass().getName());
        }
    }

    public static void main(String[] args) {
        new WordLink();
    }
}